<template>
	<view class="page">
		<view class="header" id="header">
			<u-navbar back-text="取消" :border-bottom="false" :background="{'backgroundColor': '#ededed'}" title="选择位置">
			</u-navbar>
			<view class="search-box">
				<view class="search"><input class="text-center" @input="getSearchNearbyAddress" v-model="keywords" placeholder="🔍 搜索附近位置" /></view>
			</view>
		</view>
		<view :style="{ height: height + 'px' }"></view>
		<view class="content-box">
			
			<!-- <view class="item-map font-label-color justify-center" @click="noSelectAddress">
				<view class="address text-ellipsis">不显示位置</view>
			</view> -->
			
			<view class="item-map" v-for="(item,index) in addressList" :key="index" @click="selectAddress(item)">
				<view class="address text-ellipsis">{{ item.name }}</view>
				<view class="address-desc text-ellipsis">{{ item.address }}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				background: {
					backgroundColor: '#001f3f',
				},
				keywords: '', //搜索name
				adcode: '', // 地区码
				longitude: '', //经度
				latitude: '',  //纬度
				height: 0,
				
				addressList: []
			}
		},
		onLoad() {
			uni.showLoading()
			this.getUserAddress()
		},
		mounted() {
			let self = this
			const query = uni.createSelectorQuery().in(this);
			            query.select('#header').boundingClientRect(data => {
							self.height = data.height
			            }).exec();
		},
		methods: {
			// 根据经纬度获取当前城市
			getUserAddress() {
				const self = this;
				uni.getLocation({
					altitude: true,
					success(res) {
						let { longitude, latitude} = res;
						self.longitude = longitude;
						self.latitude = latitude
						uni.request({
							url: 'https://restapi.amap.com/v3/geocode/regeo',
							data: {
								key: self.$wanlshop.config('amapkey'),
								location: `${longitude},${latitude}`
							},
							success(res2) {
								if(res2.statusCode == 200 && res2.data.status == 1) {
									let { regeocode : { addressComponent , formatted_address}  } = res2.data
									self.adcode = addressComponent.adcode;
									let name = formatted_address;
									self.getSearchNearbyAddress(name);
								}
							}
						})
					}
				})
			},
			
			// 根据abcode获取附近位置
			getSearchNearbyAddress(name = null) {
				let { keywords, longitude, latitude, adcode } = this;
				uni.request({
					url: 'https://restapi.amap.com/v3/assistant/inputtips',
					data: {
						key: this.$wanlshop.config('amapkey'),
						keywords: keywords || name,
						location: `${longitude},${latitude}`,
						city: adcode,
						datatype:'all'
					},
					success: (res) => {
						uni.hideLoading()
						let addressList = [];
						res.data.tips.forEach(item => {
							if (item.address.length == 0) {
								item.address = '暂未收录地址';
								item.location = '113.294701,22.666562';
							}
							addressList.push(item);
						});
						this.addressList = addressList
					}
				});
			},
			
			//选择位置
			selectAddress(item) {
				let pages = getCurrentPages();
				let page = pages[pages.length - 2];
				page.$vm.address = item.name;
				this.navBack();
			},
			
			//不显示位置
			noSelectAddress() {
				let pages = getCurrentPages();
				let page = pages[pages.length - 2];
				page.$vm.address = '';
				this.navBack();
			},
			
			//返回页面
			navBack() {
				uni.navigateBack()
			}
		}
	}
</script>

<style scoped>
	.page {
		position: absolute;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		width: 100%;
		height: 100%;
		background: #FFFFFF !important;
		overflow-y: scroll;
	}

	.header {
		width: 100%;
		background-color: #ededed;
		position: fixed;
		z-index: 9;
	}

	.search-box {
		padding: 20rpx 15rpx;
		width: 100%;
		box-sizing: border-box;
	}

	.search {
		height: 70rpx;
		width: 100%;
		background-color: white;
		border-radius: 8rpx;
	}
	.search input {
		width: 100%;
		height: 100%;
		padding: 0 15rpx;
		box-sizing: border-box;
		font-size: 30rpx;
	}
	.text-align {
		text-align: center;
	}
	.content-box {
		padding: 0 30rpx;
		display: flex;
		flex-direction: column;
		background-color: white;
	}
	.item-map {
		display: flex;
		flex-direction: column;
		width: 100%;
		border-bottom: solid 1rpx #f0f0f0;
		height: 98rpx;
		padding: 15rpx 145rpx 15rpx 0;
		box-sizing: border-box;
	}
	.border-top {
		border-top: solid 1rpx #f0f0f0;
	}
	.address {
		width: 100%;
		font-size: 30rpx;
		font-weight: 500;
		line-height: 30rpx;
	}
	.address-desc {
		font-size: 22rpx;
		color: #616161;
		line-height: 22rpx;
		margin-top: auto;
	}
	.text-ellipsis {
		overflow: hidden;
		text-overflow:ellipsis;
		white-space: nowrap;
	}
	.font-label-color {
		color: #495e8c;
	}
	.justify-center {
		justify-content: center;
	}
</style>
